﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel;
using FlightSearchServer;

namespace Client
{
    class Client
    {
        private const int SEARCHSERVER_URL_ARG = 0;

        private const int COMMAND_INDX = 0;

        private const int SOURCE_INDX = 1;

        private const int DESTINATION_INDX = 2;

        private const int DATE_INDX = 3;

        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine( "Incorrect input format.");
                Console.Error.WriteLine( "Expecting: <search server url>");
                return;
            }
            Console.WriteLine("Client");
            Console.WriteLine( "Press any key to start");
            Console.ReadLine();
            string line = "";
            while (!line.Equals( "EXIT"))
            {
                Console.Write( ">$ ");
                line = Console.ReadLine();
                string[] data = line.Split( ' ');
                switch (data[COMMAND_INDX])
                {
                    case "Search":
                    {
                        if (data.Length != 4)
                        {
                            Console.WriteLine("Incorrect input");
                            continue;
                        }
                        Uri serverUrl = new Uri( args[0]);
                        EndpointAddress endpoint = new EndpointAddress( serverUrl);
                        SearchServiceClient proxy = new SearchServiceClient(new BasicHttpBinding(), endpoint);
                        FlightRoutes routes = proxy.search(data[SOURCE_INDX], data[DESTINATION_INDX], data[DATE_INDX]);
                        Console.WriteLine( "{0} routes found: ", routes.Count);
                        foreach (Route route in routes)
                        {
                            switch (route.Type)
                            {
                                case RouteType.Direct:
                                {
                                    Console.WriteLine( "direct: {0}-{1} ({2}, {3})", route.FirstLegFrom,
                                        route.FirstLegTo, route.FirstLegFlightNo, route.FirstLegDate);
                                    break;
                                }
                                case RouteType.InDirect:
                                {
                                    Console.WriteLine("indirect: {0}-{1} ({2}, {3}) {4}-{5} ({6}, {7})", route.FirstLegFrom,
                                        route.FirstLegTo, route.FirstLegFlightNo, route.FirstLegDate, route.SecondLegFrom,
                                        route.SecondLegTo, route.SecondLegFlightNo, route.SecondLegDate);
                                    break;
                                }
                            }
                        }
                        break;
                    }
                    case "Exit":
                    {
                        return;
                    }
                    default:
                    {
                        Console.WriteLine( "Invalid command.");
                        break;
                    }
                }
            }
        }
    }
}
